package com.yuwenxin.dao.impl;

import com.yuwenxin.bean.Comment;
import com.yuwenxin.bean.Music;
import com.yuwenxin.bean.User;
import com.yuwenxin.core.BaseDaoImpl;
import com.yuwenxin.core.BaseQueryModel;
import com.yuwenxin.dao.CommentDao;
import com.yuwenxin.dao.MusicDao;
import com.yuwenxin.dao.UserDao;

import java.sql.ResultSet;
import java.sql.Date;

public class CommentDaoImpl extends BaseDaoImpl implements CommentDao {
    @Override
    public String getSelectAllSql() {
        return "select * from comment";
    }

    @Override
    public String getSelectByNameSql() {
        return "select * from comment where commentName = ?";
    }

    @Override
    public String getFuzzyFindSql() {
        return "select * from comment where commentName like ?";
    }

    @Override
    public String getUpdateSql(Object object) {
        return null;
    }

    @Override
    public String getDeleteSql() {
        return "delete from comment where idcomment = ?";
    }

    @Override
    public String getSelectByIdSql() {
        return "select * from comment where idcomment = ?";
    }

    @Override
    public String getInsertSql(Object object) {
        Comment comment = (Comment) object;
        String context = comment.getContext();
        Date date = new Date(new java.util.Date().getTime());  // 直接获取系统当前时间作为comment的发布时间
        return "insert into comment (context,commentTime) values ('"
                + context + "','" + date + "')";
    }

    @Override
    public String getConditionSql(BaseQueryModel queryModel) {
        return null;
    }

    @Override
    public Object toEntity(ResultSet rs) throws Exception {
        Comment comment = new Comment();

        UserDao userDao = new UserDaoImpl();
        MusicDao musicDao = new MusicDaoImpl();

        comment.setId(rs.getInt("idcomment"));
        comment.setContext(rs.getNString("context"));
        comment.setCommentTime(rs.getTime("commentTime"));
        comment.setCommentUserId(rs.getInt("commentUserid"));
        comment.setCommentMusicId(rs.getInt("commentMusicid"));
        comment.setPoster((User) userDao.find(rs.getInt("commentUserid")));
        comment.setCommentedMusic((Music) musicDao.find(rs.getInt("commentMusicid")));
        return comment;
    }
}
